﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Collections.Generic;
using System.Text;

public partial class AvailabilityCheck : System.Web.UI.Page
{
    private string _muztorgUri = "http://www.muztorg.ru/catal/nalich?id={0}";
    private List<ShopAvailability> shopList = new List<ShopAvailability>();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        using (WebClient wc = new WebClient())
        {
            try
            {
                wc.Encoding = Encoding.GetEncoding("Windows-1251");
                string availability = wc.DownloadString(string.Format(_muztorgUri, Request.QueryString["id"]));
                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(availability);
                doc.OptionOutputAsXml = true;
                string n = GetValidXML(doc.DocumentNode.ChildNodes[3].ChildNodes[3].ChildNodes[8].OuterHtml);
                
                XDocument x = XDocument.Parse(n);
                
                var avvail = from td in x.Descendants("td")
                             where td.Element("img") != null && td.Element("img").Attribute("src").Value != "../img/px.gif"
                             select td.Element("img").Attribute("src").Value;
                var cities = from td in x.Descendants("td")
                             where td.Element("a") != null
                             select td.Element("a").Value;

                IEnumerator<string> cityEnum = cities.GetEnumerator();
                foreach (string s in avvail)
                {
                    cityEnum.MoveNext();
                    shopList.Add(new ShopAvailability(cityEnum.Current, s));
                }
                if (Roles.IsUserInRole("Administrator"))
                {
                    AvailableList.DataSource = shopList;
                    AvailableList.DataBind();
                }
                else
                {
                    for (int i = 0; i < 2; i++)
                    {
                        if (shopList[i].IsAvailable)
                        {
                            SuccessImage.Visible = true;
                            break;
                        }
                    }
                    if (SuccessImage.Visible)
                    {
                        AvailabilityMessage.Text = Resources.Notifications.ProductAvailable;
                    }
                    else
                    {
                        WarningImage.Visible = true;
                        AvailabilityMessage.Text = Resources.Notifications.ProductUnavailable;
                    }
                }
            }
            catch (Exception)
            {
                ErrorImage.Visible = true;
                ErrorMessage.Text = Resources.ExceptionMessages.AvailabilityCheckException;
            }
        }
    }
    private string GetValidXML(string htmlCode)
    {
        htmlCode = htmlCode.Replace("height=\"12\">", "/>");
        htmlCode = htmlCode.Replace("border=\"0\">", "/>");
        htmlCode = htmlCode.Replace("height=\"2\">", "/>");
        return htmlCode;
    }
}
